//require cn1-codescan
package com.codename1.samples;


import static com.codename1.ui.CN.*;

import com.codename1.components.SpanLabel;
import com.codename1.ext.codescan.CodeScanner;
import com.codename1.ext.codescan.ScanResult;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Button;
import com.codename1.ui.ButtonGroup;
import com.codename1.ui.Container;
import com.codename1.ui.RadioButton;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.Log;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class CodeScannerSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        final Form hi = new Form("Codescan Demo");
        hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        ButtonGroup bg = new ButtonGroup();
        final RadioButton qr = new RadioButton("QR Code");
        final RadioButton bar = new RadioButton("Bar Code");
        bg.add(qr);
        bg.add(bar);
        hi.addComponent(new Label("Code Type"));
        hi.addComponent(qr);
        hi.addComponent(bar);
        
        Button scanBtn = new Button("Scan Code");
        scanBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                final Container cnt = hi;

                if(qr.isSelected()){
                    CodeScanner.getInstance().scanQRCode(new ScanResult() {

                        public void scanCompleted(String contents, String formatName, byte[] rawBytes) {
                            //barCode.setText("Bar: " + contents);
                            cnt.addComponent(new Label(contents));
                            cnt.revalidate();
                        }

                        public void scanCanceled() {
                            System.out.println("cancelled");
                        }

                        public void scanError(int errorCode, String message) {
                            System.out.println("err " + message);
                        }
                    });
                }else{
                    CodeScanner.getInstance().scanBarCode(new ScanResult() {

                        public void scanCompleted(String contents, String formatName, byte[] rawBytes) {
                            //barCode.setText("Bar: " + contents);
                            cnt.addComponent(new Label(contents));
                            cnt.revalidate();
                        }

                        public void scanCanceled() {
                            System.out.println("cancelled");
                        }

                        public void scanError(int errorCode, String message) {
                            System.out.println("err " + message);
                        }
                    });        
                }
            }
            
        });
        if (CodeScanner.isSupported()) {
            hi.addComponent(scanBtn);
        } else {
            hi.addComponent(new SpanLabel("Sorry.  Codescanner not supported on this platform"));
        }
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
